package br.com.mural.android.adaptadores;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import br.com.mural.android.R;
import br.com.mural.android.entidades.Questao;

public class QuestionarioAdapter extends BaseAdapter{
	private LayoutInflater inflater;
	private List<Questao> questoes;
	LinearLayout layoutObj, layoutSub;
	Button btnObj, btnSub, btnResposta;
	EditText respostaSub;
	RatingBar respostaObj;
	ItemSuporte itemHolder;
	TextView resposta;
	Questao item;
	Bundle bundle;
	
	public QuestionarioAdapter(Context context, List<Questao> questoes) {
		this.questoes = questoes;
		inflater = LayoutInflater.from(context);
	}
	
	
	public QuestionarioAdapter(Context context, List<Questao> questoes, Bundle bundle) {
		this.questoes = questoes;
		inflater = LayoutInflater.from(context);
		this.bundle = bundle;
	}
	

	@Override
	public int getCount() {
		return questoes.size();
	}

	@Override
	public Object getItem(int position) {
		return questoes.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		

		if (view == null) {
			view = inflater.inflate(R.layout.item_questionario, null);

			itemHolder = new ItemSuporte();
			itemHolder.txtNumeroPergunta = (TextView) view.findViewById(R.id.numeroPergunta);
			itemHolder.txtPergunta = (TextView) view.findViewById(R.id.pergunta);
			//itemHolder.editResposta = (EditText) view.findViewById(R.id.editResposta);
			//layoutObj = (LinearLayout) view.findViewById(R.id.resposta);
			//layoutSub = (LinearLayout) view.findViewById(R.id.respostaSub);
			//respostaSub = (EditText) view.findViewById(R.id.respostaSubjetiva);
			//btnObj = (Button) view.findViewById(R.id.btnObj);
			//btnSub = (Button) view.findViewById(R.id.btnSub);
			respostaObj = (RatingBar) view.findViewById(R.id.respostaObj);
			respostaSub = (EditText) view.findViewById(R.id.respostaSub);
			//btnResposta = (Button) view.findViewById(R.id.btnResponder);
			
			
			/*respostaObj.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
				public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {
		 
					Toast.makeText(inflater.getContext(), "Clicou:"+ String.valueOf(rating) + " - " + itemHolder.txtPergunta.getText().toString(), Toast.LENGTH_SHORT).show();
					
					bundle.putString (itemHolder.txtPergunta.getText().toString(), String.valueOf(rating));

		 
				}
			});*/
			
			
			
			
			
			
			
			view.setTag(itemHolder);
		} else {

			itemHolder = (ItemSuporte) view.getTag();
		}

		item = questoes.get(position);
		
		itemHolder.txtNumeroPergunta.setText(position + 1 +". ");
		itemHolder.txtPergunta.setText(item.getDescricao());
		bundle.putString(item.getDescricao(), String.valueOf(item.getId()));
		
		

		
		if (position % 2 == 0) {
			view.setBackgroundColor(Color.GRAY);
		} else {
			view.setBackgroundColor(Color.LTGRAY);
		}
		
		if(item.getClass().getSimpleName().equals("QuestaoObjetiva")){
			//view.setBackgroundColor(Color.BLUE);
			respostaObj.setVisibility(View.VISIBLE);
			respostaSub.setVisibility(View.INVISIBLE);
			
			/*respostaObj.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
				public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {
		 

					
					bundle.putString (item.getDescricao(), String.valueOf(rating));

		 
				}
			});*/
			
			
		}else{
			if(item.getClass().getSimpleName().equals("QuestaoSubjetiva")){
				//view.setBackgroundColor(Color.RED);
				respostaSub.setVisibility(View.VISIBLE);
				respostaObj.setVisibility(View.INVISIBLE);
				
				//bundle.putString (item.getDescricao(), respostaSub.getText().toString());

				
			}
	
		
		
			

			
			
		}
		

		return view;
	}
	
	public Bundle getBundle(){
		bundle.putString("1", "2222222");
		return bundle;
	}

	public class ItemSuporte {
		TextView txtNumeroPergunta;
		TextView txtPergunta;
		EditText respostaSub;
		RatingBar respostaObj;
		//TextView resposta;
		//EditText editResposta;
		
	}

}
